<?php
use app\common\controller\Api;
use app\common\controller\Redis;
use think\Db;
function is_del_avideo($uid){
    $redis = Redis::getInstance()->getRedisConn();
    $reds=$redis->hGetAll('user_avudeo_'.$uid);
    return array_keys($reds);
}
function is_del_exam($uid){
    $redis = Redis::getInstance()->getRedisConn();
    $reds=$redis->hGetAll('user_exam_'.$uid);
    return array_keys($reds);
}
function gettop($cate,$id){
    $topid=0;
    foreach ($cate as $k=>$v){
        if($id!=0&&$id==$cate[$k]['id']){
            if($cate[$k]['pid']==0){
                $topid=$cate[$k]['id'];
            }else{
                $topid=gettop($cate,$cate[$k]['pid']);
            }
        }
    }
    return $topid;
}
/*
 * @param $array
 * @param $id
 * @return array
 * 下级id组合
 */
function get_all_child($array, $id)
{
    $arr = array();
    foreach ($array as $v) {
        if ($v['pid'] == $id) {
            $arr[] = $v['id'];
            $arr = array_merge($arr, get_all_child($array, $v['id']));
        };
    };
    return $arr;
}
function catearr($array, $id){
    $arr = array();
    foreach ($array as $k=>$v) {
        if ($array[$k]['pid'] == $id) {
            $v['children'] = catearr($array, $v['id']);
            $arr[] = $v;
        };
    };
    return $arr;
}

/*
 * @param $uid
 * @return int
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * 该用户的公司id
 */
function gongsi($uid){
    $category = Db::name('category')->field('id,name,pid')->select()->toArray();
    $auser = Db::name('auser')->where('id', $uid)->find();
    $pid=gettop($category,$auser['cate_id']);
    return $pid;
}

/*
 * @param $pid
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * pid的下面所有部门
 */
function pid_allcate($pid){
    $category = Db::name('category')->field('id,name,pid')->select()->toArray();
    $ids=get_all_child($category,$pid);
    return $ids;
}

/*
 * @param $category
 * @param $id
 * @return array
 * 获取id的上级直到pid=0
 */
function all_cate($category,$id){
    $arr = array();
    foreach ($category as $k=>$v){
        if($category[$k]['id']==$id){
            if ($v['pid'] != 0) {
                $arr[] = $v['id'];
                $arr = array_merge($arr, all_cate($category, $v['pid']));
            }else{
                $arr[] = $v['id'];
            }
        }
    }
    return $arr;
}

/*
 * @param $id
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * 获取当前cate_id用户的的所有上级的所有id
 */
function pid_cate($id){
    $category = Db::name('category')->field('id,name,pid')->select()->toArray();
    $arr=all_cate($category,$id);
    return $arr;
}

/*
 * @param $uid
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * 获取该用户id的所有可以看的视频id
 */
function cate_avideo($uid){
    $cid = Db::name('auser')->where('id',$uid)->value('jobs_id');
    $avideos=Db::name('avideo')->field('cate_id,id')->select();
    $avideo_ids=array();
    $cateids=pid_cate($cid);
    foreach ($avideos as $k=>$v){
        $cates=explode(',',$avideos[$k]['cate_id']);
        if(!empty(array_intersect($cateids,$cates))){
            $avideo_ids[]=$avideos[$k]['id'];
        }
    }
    return $avideo_ids;
}

/*
 * @param $perPage
 * @param $page
 * @param $array
 * @return array
 * 数组分页
 */
function page_array($perPage,$page,$array){
    $page=(empty($page))?'1':$page; #判断当前页面是否为空 如果为空就表示为第一页面
    $start=($page-1)*$perPage; #计算每次分页的开始位置
    $pagedata=array();
    $pagedata=array_slice($array,$start,$perPage);    //分隔数组
    return $pagedata;  #返回查询数据
}

